package com.topnav.mc.runner;

import com.topnav.mc.admin.entity.GbQuartzJob;
import com.topnav.mc.admin.service.IGbQuartzService;
import com.topnav.mc.involve.solve.SubscribePosition;
import com.topnav.mc.schedule.JobManagement;
import com.topnav.mc.schedule.job.SubscribePositionJob;
import com.topnav.mc.schedule.task.QuartzTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *
 * @类名: ScheduleRunner
 * @说明: 启动运行
 *
 * @author: kenny
 * @Date	2020年3月25日下午3:49:40
 * 修改记录：
 *
 * @see
 */
@Order(2)
@Component
@Slf4j
public class ScheduleRunner implements ApplicationRunner{
	/** 强制GC */
	@Value("${cron.refresh-state:*/30 * * * * ?}")
	private String cronGc;

	//多久时间刷新一次位置 */
	@Value("${cron.refresh-position:0 */2 * * * ?}")
	private String cronRefreshPosition;

	//订时给服务端发送位置订阅
	//@Value("${cron.subscribe-position:0 */5 * * * ?}")
	//private String cronSubscribePosition;

	// 国标服务器在线信息更新
	@Value("${cron.refresh-gbserver-state:0 */5 * * * ?}")
	private String cronUpdateGbServerState;

	/** 系统监控，内存 CPU 存储 */
	@Value("${cron.monitor-cpu-memory:*/5 * * * * ?}")
	private String cronMonitorCpuMemory;

	/** 系统监控 存储 */
	@Value("${cron.monitor-storage:0 */5 * * * ?}")
	private String cronMonitorStorage;

	/** 定时清理过期信息 每日两点 */
	@Value("${cron.clear-expired-data:0 0 2 * * ? *}")
	private String cronClearExpiredData;

	@Autowired
	private SubscribePositionJob subscribePositionJob;

	@Autowired
	private SubscribePosition subscribePosition;

	//0 0/1 * * * ? *

	//GbServerOnlineCheckJob

	@Autowired
	private IGbQuartzService gbQuartzService;

	@Autowired
	private JobManagement jobManagement;

	private String scheduleStr = ">>>>>> 启动 %s 定时任务...... ";

	@Override
	public void run(ApplicationArguments args) throws Exception {
		/** 默认任务 */
		setDefaultJob();

		/** 查询所有的 JOB */
		List<GbQuartzJob> quartzJobList = gbQuartzService.list();
		for(GbQuartzJob job : quartzJobList) {
			if (job.getStatus() != 1) {
				continue;
			}

			log.info(String.format(">>>>>>>>注册%s>>>>>>>>",scheduleStr,job.getJobDesc()));
			jobManagement.addJob(job.toTask());
		}





/*		System.out.println("定时刷新状态");
		QuartzTask jstask = new QuartzTask()
				.setJobName("job_2")
				.setJobGroupName("job_group_2")
				.setTriggerName("triger_2")
				.setTriggerGroupName("triger_group_2")
				.setCron(cronRefreshStatus)
				.setJobClass("com.topnav.mc.schedule.job.RefreshDeviceStatusJob");
		jobManagement.addJob(jstask);*/



		/** 启动定时任务 */
		jobManagement.start();

		// 先跑一次位置订阅
		subscribePosition.subscribe(subscribePositionJob.expires,subscribePositionJob.interval);
	}

	/**
	 * 默认任务，用户不能修改的任务
	 */
	private void setDefaultJob(){
		log.info(String.format(scheduleStr,">>>>>>>>强制GC>>>>>>>>"));
		QuartzTask taskGc = new QuartzTask()
				.setJobName("job_gc")
				.setJobGroupName("job_group_gc")
				.setTriggerName("triger_gc")
				.setTriggerGroupName("triger_group_gc")
				.setCron(cronGc)
				.setJobClass("com.topnav.mc.schedule.job.ForcedGcJob");
		jobManagement.addJob(taskGc);

/*		log.info(String.format(">>>>>>>>注册定时刷新位置>>>>>>>>"));
		QuartzTask mdtask = new QuartzTask()
				.setJobName("job_default_2")
				.setJobGroupName("job_group_default_2")
				.setTriggerName("triger_2")
				.setTriggerGroupName("triger_group_2")
				.setCron(cronSubscribePosition)
				.setJobClass("com.topnav.mc.schedule.job.SubscribePositionJob");
		jobManagement.addJob(mdtask);*/

		log.info(String.format(">>>>>>>>注册定时更新国标服务器状态>>>>>>>>"));
		QuartzTask mdtask3 = new QuartzTask()
				.setJobName("job_default_3")
				.setJobGroupName("job_group_default_3")
				.setTriggerName("triger_3")
				.setTriggerGroupName("triger_group_3")
				.setCron(cronUpdateGbServerState)
				.setJobClass("com.topnav.mc.schedule.job.GbServerOnlineCheckJob");
		jobManagement.addJob(mdtask3);

		log.info(String.format(">>>>>>>>监控系统的CPU 内存使用情况 >>>>>>>>"));
		QuartzTask mdtask4 = new QuartzTask()
				.setJobName("job_default_4")
				.setJobGroupName("job_group_default_4")
				.setTriggerName("triger_4")
				.setTriggerGroupName("triger_group_4")
				.setCron(cronMonitorCpuMemory)
				.setJobClass("com.topnav.mc.schedule.job.SystemInfoMonitorJob");
		jobManagement.addJob(mdtask4);

		log.info(String.format(">>>>>>>>监控系统的存储使用情况 >>>>>>>>"));
		QuartzTask mdtask5 = new QuartzTask()
				.setJobName("job_default_5")
				.setJobGroupName("job_group_default_5")
				.setTriggerName("triger_5")
				.setTriggerGroupName("triger_group_5")
				.setCron(cronMonitorStorage)
				.setJobClass("com.topnav.mc.schedule.job.StorageInfoMonitorJob");
		jobManagement.addJob(mdtask5);


		//清理过期数据
		log.info(String.format(">>>>>>>>Schedule: Clear Expired Data >>>>>>>>"));
		QuartzTask mdtask6 = new QuartzTask()
				.setJobName("job_default_6")
				.setJobGroupName("job_group_default_6")
				.setTriggerName("triger_6")
				.setTriggerGroupName("triger_group_6")
				.setCron(cronClearExpiredData)
				.setJobClass("com.topnav.mc.schedule.job.ClearExpiredDataJob");
		jobManagement.addJob(mdtask6);

	}


}
